Color Images

In this module, we will cover the most fundamental concepts associated with color images. These include color spaces, color channels, and some practical considerations associated with reading and displaying color images.

1. Reading and Displaying Color Images

Until now, we have been using grayscale images in our discussion. Let's now start working with color images. We will begin by reading and displaying color images in two different formats. We will also discuss some of the unexpected results that can occur.

1.1 Example: Facebook Logo (JPG)

We will start with a simple color image below of the Facebook logo in a JPG format.

c0-m1-Facebook-logo.jpg

Read JPG image and print the shape and data type

Color images are typically represented using three separate "color" channels. The specific color channels used to represent a color image depend on the Color Space. One of the most common color spaces is the RGB color space, which contains Red, Green, and Blue channels.

Display the image

What happened to the color?

The color displayed above is different from the actual image. This is because matplotlib expects the image to be in RGB format whereas OpenCV stores images in BGR format. Thus, for correct display, we need to reverse the channel order of the image in order to properly render the color of the image.

Swap the Red and Blue channels

There are a couple of different approaches to reversing the order of the color channels. The first approach shown below uses a short-hand NumPy array slicing syntax that will reverse the order of the channels in the 3rd dimension of the image array.

1.2 Example : PyTorch Logo (PNG)

Let's now take a look at a PNG image of the PyTorch logo.

c0-m1-Pytorch-logo.png

Read and display a PNG image

What happened?

The color channels need to be swapped as in the previous example, but there is also a black background that was unexpected.

Use cv2.IMREAD_UNCHANGED to read the image with the alpha channel

PNG images support a 4th channel called the "alpha" channel. The alpha channel contains transparency information that allows specific regions within an image to appear transparent. As an example, consider the Facebook logo in the previous section. The logo contains two colors (blue and white). The white letters in the logo are actually white: (255, 255, 255). The PyTorch logo, on the other hand, contains an alpha channel that allows certain regions of the image to appear transparent. So the "white" background is not white. Instead, those pixels are being masked by a 4th (alpha) channel, and are interpreted as transparent. In this case the pixels in the background portion of the image are set to: (0,0,0), which will appear as black unless we include the alpha chanel to mask them. We will cover transparency and alpha masking in a future module in more detail, but it is important to be aware of these details when reading and displaying images.


cvtColor() Converts an image from one color space to another. Note that the default color format in OpenCV is often referred to as RGB but it is actually BGR (the bytes are reversed). So the first byte in a standard (24-bit) color image will be an 8-bit Blue component, the second byte will be Green, and the third byte will be Red. This function can be used to simply swap the order of the Blue and Red channels for the RGB color space, but it can also be used to convert between color spaces as we will see further below.

Function Syntax

dst = cv2.cvtColor(src, code)

dst: Is the output image of the same size and depth as src.

The function has 2 required arguments:

  1. src: input image: 8-bit unsigned, 16-bit unsigned ( CV_16UC... ), or single-precision floating-point.
  2. code: color space conversion code (see ColorConversionCodes below).

OpenCV Documentation

cvtColor() ColorConversionCodes()


Use cvtColor() to convert BGRA to RGBA

We previously showed that swapping the Blue and Red channels can be accomplished using NumPy as follows: img[:, :, ::-1], however, there is a method in OpenCV that can be used for this purpose as well as many other color conversions. Let's use the cvtColor() to swap the channel order.

2. Splitting and Merging Color Channels

Let's now learn how to split and merge color channels using the split() and merge() functions in OpenCV.


split() Divides a multi-channel array into several single-channel arrays.

merge() Merges several arrays to make a single multi-channel array. All the input matrices must have the same size.

OpenCV Documentation

split() merge()


In the example below, we will read an image, split the color channels and plot the individual color channels of the grayscale image to better understand how the individual channels contribute to the color in the original image.

3. Converting to Different Color Spaces

3.1 Background on Color Spaces

In simple terms, a color space is a specific organization of colors that typically represents the space of all possible human-perceivable colors. A color model is a mathematical construct for how to specify colors in the color space with a unique tuple of numbers (typically as three or four values representing the relative contributions of color components). A color model can be thought of as a mathematical way to navigate a color space. However, it is very common to use the term “color space” to collectively define both a color model along with a specific mapping of that model onto an absolute color space.

As an introduction to color spaces we will consider two commonly used models: the RGB color space (for Red, Green, Blue) and the HSV color space (for Hue, Saturation, Value). Both color spaces use a three-dimensional coordinate system to specify the component colors that represent a unique tuple, and therefore a unique color. These components are also referred to as color channels. Since color images are typically represented by three color channels as 8-bit unsigned integers for each channel, the individual color components can take on values from [0,255]. So we can therefore represent 16.77 Million unique colors in either color space (256 256 256).

In the examples below, we will be working with color images in RGB and HSV.

3.2 Changing to HSV Color Space

We can also use cvtColor() to convert from one color space to another. In the example below, we will convert the image data to the HSV color space, split the channels, and display the individual channels as grayscale images.

3.3 Modifying Individual Color Channels

4. Saving Images [recap]


Saving images using OpenCV is very straightforward using the function imwrite(). The function saves the image to the specified file. The image format is chosen based on the filename extension (see imread() for the list of extensions). In general, only 8-bit single-channel or 3-channel (with 'BGR' channel order) images can be saved using this function (see the OpenCV documentation below for further details).

Function Syntax

cv2.imwrite(filename, img[, params])

The function has 2 required arguments:

  1. filename: This can be an absolute or a relative path.
  2. img: Image or Images to be saved.

OpenCV Documentation

imwrite() imwriteFlags()


Saving color images

5. Exercise

Please complete the code in the cell below.

Your results should look similar to this.

c0-m1-Emerald-lake-Exercise-01-02.png